/**
 * Created by dukes on 2015/10/28.
 */
//module game{
    class DecorateBuyPanel extends eui.Component{
        public constructor(){
            super();
            this.skinName = "skins.scene.DecorateBuyListSkin";
        }
        private itemlist:eui.List;
        private img_yes:eui.Image;
        private img_close:eui.Image;
        public partAdded(name:string, instance:any){
            super.partAdded(name, instance);
            if( name == "itemlist"){
                this.itemlist = instance;
            }else if( name == "img_yes"){
                this.img_yes = instance;
                this.img_yes.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBuy, this);
            }else if( name == "img_close" ){
                this.img_close = instance;
                this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.close, this);
            }
        }
        public close(evt:egret.TouchEvent){
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            this.hide();
        }
        public onBuy(evt:egret.TouchEvent){
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            var arr = this.listProvider.source;
            var len = arr.length;
            var good:GoodInfo;
            var diamonCost = 0;
            var catHairCost = 0;
            var dogHairCost = 0;
            var mengCost = 0;
            for(var i = 0 ; i < len; i++){
                good = Model.GetDecorateById(arr[i]);

                if( good.byDiamond ){
                    diamonCost += good.disPrice;
                }else if(good.bySproutPoint ){
                    mengCost += good.disPrice;
                }else{
                    if( good.byCatHair && Model.me.myType == Model.GOOD_TYPE_CAT){
                        catHairCost += good.hair;
                    }else if( good.byDogHair && Model.me.myType == Model.GOOD_TYPE_DOG){
                        dogHairCost += good.hair;
                    }
                }
            }

            if( Model.me.diamond < diamonCost){
                this.hide();
                BuyDiamondPanel.getInstance().show();
            }else if( Model.me.dogHair < dogHairCost){
                ShowHint("多产点毛再买吧");
            }else if( Model.me.catHair < catHairCost){
                ShowHint("多产点毛再买吧");
            }else if( Model.me.sproutPoint < mengCost){
                ShowHint("多攒点萌点再买吧");
            }else{
                if( diamonCost != 0){
                    this.hide();
                    RemindPanel.getInstance().showContent("您本次需要花费"+diamonCost+"钻石，是否购买？",this,this.okBuy);
                }else{
                    this.okBuy();
                }
            }
            Model.checkGuide("comfirm_list");
        }
        public okBuy(){
            this.hide();
            if( this.listProvider.length!=0)
                window["__buydecorates"](PetMgr.getInstance().curPet.petId,JSON.stringify(this.listProvider.source),1);
        }
        public deleteItemByIndex(index){
            this.listProvider.removeItemAt(index);
        }
        private listProvider:eui.ArrayCollection;
        public show(src:any[]=null){
            DisplayController.instance().addToAlert(GameConst.GrayMask);
            DisplayController.instance().addToAlert(this);
            if( !src ) return;
            if( this.listProvider == null){
                this.listProvider = new eui.ArrayCollection();
                this.itemlist.dataProvider = this.listProvider;
                this.itemlist.itemRenderer = DecBuyItemRender;
            }
            this.listProvider.source = src;
        }
        public hide(){
            DisplayController.instance().removeFromAlert(GameConst.GrayMask);
            DisplayController.instance().removeFromAlert(this);
        }

        public measure(){
            super.measure();
            this.x = GameConst.StageW - this.measuredWidth >> 1;
            this.y = GameConst.StageH - this.measuredHeight >> 1;
        }

        private static _instance:DecorateBuyPanel;
        public static getInstance():DecorateBuyPanel{
            if( DecorateBuyPanel._instance == null)
                DecorateBuyPanel._instance = new DecorateBuyPanel();
            return DecorateBuyPanel._instance;
        }
    }
//}